// <editor-fold defaultstate="collapsed" desc="GNU GPLv3 Header">
/*
 * File:   Main.java
 *
 * Copyright (C) 2013 Robert Antoni Buj Gelonch <rbuj@uoc.edu>
 * Copyright (C) 2013 David Megias Jimenez <dmegias@uoc.edu>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
// </editor-fold>
package readwritewave;

// <editor-fold defaultstate="collapsed" desc="imports">
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
// </editor-fold>

/**
 * RIFF WAVE (.WAV) file.
 *
 * Class for reading and writing RIFF WAVE audio files.
 *
 * @author Robert Antoni Buj Gelonch <rbuj@uoc.edu>
 * @version 1.0
 * @since 2013-08-04
 */
class WaveFile {

    private String strSrcFilePath;
    /**
     * WAVE Header
     */
    private HeaderWaveFile headerWaveFile;
    /**
     * WAVE Format Chunk
     */
    private GenericFormatChunkWaveFile formatChunkWaveFile;
    /**
     * WAVE Data Chunk
     */
    private GenericDataChunkWaveFile dataChunkWaveFile;

    /**
     * This constructor is used for reading the audio information from an input
     * wave file.
     *
     * @param str the path to a wave file
     *
     * @author Robert Antoni Buj Gelonch <rbuj@uoc.edu>
     * @version 1.0
     * @since 2013-08-04
     */
    WaveFile(String str) throws FileNotFoundException, IOException {
        strSrcFilePath = str;
        FileInputStream fis = new FileInputStream(strSrcFilePath);
        DataInputStream dis = new DataInputStream(fis);
        headerWaveFile = new HeaderWaveFile(dis);
        GenericFormatChunkWaveFile genericFormatChunkWaveFile = new GenericFormatChunkWaveFile(dis);
        if (genericFormatChunkWaveFile.formatIsPCM()) {
            formatChunkWaveFile = new PCMFormatChunkWaveFile(genericFormatChunkWaveFile, dis);
            GenericDataChunkWaveFile genericDataChunkWaveFile = new GenericDataChunkWaveFile(dis);
            short wBitsPerSample = ((PCMFormatChunkWaveFile) formatChunkWaveFile).getWBitsPerSample();
            if (wBitsPerSample > 8) {
                short wChannels = formatChunkWaveFile.getWChannels();
                if (wChannels == 1) {
                    dataChunkWaveFile = new PCM16DataChunkWaveFile(genericDataChunkWaveFile,
                            wBitsPerSample, dis);
                } else {
                    throw new UnsupportedOperationException("Not yet implemented,"
                            + " only supports 1 channel.");
                }
            } else {
                throw new UnsupportedOperationException("Not yet implemented, "
                        + "only supports >8bps.");
            }
        } else {
            throw new UnsupportedOperationException("Not yet implemented, only"
                    + " supports Microsoft Pulse Code Modulation (PCM) format.");
        }
        dis.close();
        fis.close();
    }

    /**
     * This function is used for storing the audio information to an output file.
     *
     * @param strDstFilePath the path to a new wave file, used to store the audio
     * data.
     * 
     * @author Robert Antoni Buj Gelonch <rbuj@uoc.edu>
     * @version 1.0
     * @since 2013-08-04
     */
    void save(String strDstFilePath) throws IOException {
        FileOutputStream fos = new FileOutputStream(strDstFilePath);
        DataOutputStream dos = new DataOutputStream(fos);
        headerWaveFile.save(dos);
        if (formatChunkWaveFile.formatIsPCM()) {
            ((PCMFormatChunkWaveFile) formatChunkWaveFile).save(dos);
            short wBitsPerSample = ((PCMFormatChunkWaveFile) formatChunkWaveFile).getWBitsPerSample();
            if (wBitsPerSample > 8) {
                short wChannels = formatChunkWaveFile.getWChannels();
                if (wChannels == 1) {
                    ((PCM16DataChunkWaveFile) dataChunkWaveFile).save(dos, wBitsPerSample);
                } else {
                    throw new UnsupportedOperationException("Not yet implemented,"
                            + " only supports 1 channel.");
                }
            } else {
                throw new UnsupportedOperationException("Not yet implemented, "
                        + "only supports >8bps.");
            }
        } else {
            throw new UnsupportedOperationException("Not yet implemented, only "
                    + "supports Microsoft Pulse Code Modulation (PCM) format.");
        }
        dos.close();
        fos.close();
    }
}
